//
//  WBVisitView.swift
//  WeiBoApp
//
//  Created by CrazyHacker on 16/6/30.
//  Copyright © 2016年 CrazyHacker. All rights reserved.
//

import UIKit
/// 访客视图
class WBVisitView: UIView {

    // 圆型图
    private lazy var functonView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 房子
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 遮罩视图
    private lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 提示label
    private lazy var tipsLabel:UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray())
   
    // 注册按钮
     lazy var registerButton:UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: #colorLiteral(red: 1, green: 0.501960814, blue: 0, alpha: 1), highlightedColor: #colorLiteral(red: 0.5296475887, green: 0.5296317339, blue: 0.5296407342, alpha: 1),backgroundImageName: "common_button_white_disable")
    // 登录按钮
     lazy var loginButton:UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: #colorLiteral(red: 0.4266758859, green: 0.4266631007, blue: 0.4266703427, alpha: 1), highlightedColor: #colorLiteral(red: 0.4266758859, green: 0.4266631007, blue: 0.4266703427, alpha: 1),backgroundImageName: "common_button_white_disable")
    var userInfo: [String: String]? {
        didSet {
            guard let imageName = userInfo?["imageName"],
                message = userInfo?["message"] else {
                    return
            }
            if imageName == "" {
                // 开始动画
                startAnimation()
                return
            }
            functonView.image = UIImage(named: imageName)
            tipsLabel.text = message
            houseIconView.isHidden = true
            maskIconView.isHidden = true
        }
    }
   
    override init(frame:CGRect) {
        super.init(frame:frame)
       
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

// 开始动画
extension WBVisitView {
    private func startAnimation() {
        let anim = CABasicAnimation.init(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        // 重复次数 -- 最大值
        anim.repeatCount = MAXFLOAT
        // 动画完成不退出 -- 切换界面保证不停止动画 -- 退出后台重新进入界面保证不停止动画
        anim.isRemovedOnCompletion = false
        anim.duration = 10
        functonView.layer.add(anim, forKey: nil)
    }
}

extension WBVisitView {
    /// 搭建访客视图界面
    func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        self.addSubview(functonView)
        self.addSubview(maskIconView)
        self.addSubview(houseIconView)
        self.addSubview(tipsLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        tipsLabel.textAlignment = .center
        for v in subviews {
            // 使用原生的布局就不能使用自动布局
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        layoutView()
    }
}

extension WBVisitView {
    func layoutView() {
        // 圆型图
        
        addConstraint(NSLayoutConstraint(item: functonView,
                                                     attribute:.centerX,
                                                     relatedBy: .equal,
                                                     toItem: self,
                                                     attribute: .centerX,
                                                     multiplier: 1.0,
                                                     constant: 0))
        
        addConstraint(NSLayoutConstraint(item: functonView,
                                                     attribute: .centerY,
                                                     relatedBy: .equal,
                                                     toItem: self,
                                                     attribute: .centerY,
                                                     multiplier: 1.0,
                                                     constant: -60))
        // 房子
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: functonView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: functonView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        // 提示label
        addConstraint(NSLayoutConstraint(item: tipsLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: functonView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipsLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        addConstraint(NSLayoutConstraint(item: tipsLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: functonView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipsLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipsLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipsLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipsLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        // 遮罩视图
        let dict = ["maskIconView": maskIconView, "registerButton": registerButton]
        let metircs = ["metircs": 20]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: dict))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(metircs)-[registerButton]", options: [], metrics: metircs, views: dict))
        
    }
}
